package com.example.task1.ui.AllService.DataAnalysis;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.task1.R;
import com.example.task1.bean.NewsListBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class DataAnalysisActivity extends AppCompatActivity {


    private BarChart chart;
    private ActionBar supportActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_analysis);
        supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("数据分析");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        intiData();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void intiData() {
        Call<ApiResp> apiRespCall = Api.api().get("/prod-api/press/press/list");
        Api.send(apiRespCall, NewsListBean.class, new CallBack<NewsListBean>() {
            @Override
            public void success(NewsListBean data) {
                List<NewsListBean.RowsBean> rows = data.getRows().subList(0, 5);
                ArrayList<BarEntry> barEntries = new ArrayList<>();
                for (int i = 0; i < rows.size(); i++) {
                    BarEntry barEntry = new BarEntry(i, rows.get(i).getLikeNum());
                    barEntries.add(barEntry);
                }
                BarDataSet barDataSet = new BarDataSet(barEntries, "新闻数据分析");
                BarData barData = new BarData(barDataSet);
                XAxis xAxis = chart.getXAxis();
                ValueFormatter valueFormatter = new ValueFormatter() {
                    @Override
                    public String getFormattedValue(float value) {
                        return rows.get((int) value).getTitle().substring(0, 5) + "...";
                    }
                };
                xAxis.setValueFormatter(valueFormatter);
                xAxis.setLabelCount(rows.size());
                chart.setData(barData);
                chart.setScaleEnabled(false);
                chart.invalidate();
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void initView() {
        chart = (BarChart) findViewById(R.id.chart);
    }
}